<?php

global $_GPC;
global $_W;
$shopid = check_right(2);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
if ($operation == 'display') {
    $goodpcate = pdo_fetchall('SELECT id,title FROM ' . tablename('j_hmoney_goodcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by id desc');
    $goodary = array();
    foreach ($goodpcate as $row) {
        $goodary[$row['id']] = $row['title'];
    }
    $goodary[0] = '未分类';
    $condition = '  ';
    $datetype = $_GPC['datetype'] ? $_GPC['datetype'] : 1;
    switch ($datetype) {
        case 1:
            $starttime = strtotime(date('Y-m-d') . ' 00:00:00');
            $endtime = strtotime(date('Y-m-d') . ' 23:59:59');
            break;
        case 2:
            $starttime = strtotime(date('Y-m-d', strtotime('-1 day')) . ' 00:00:00');
            $endtime = strtotime(date('Y-m-d', strtotime('-1 day')) . ' 23:59:59');
            break;
        case 3:
            $starttime = strtotime(date('Y-m-d', strtotime('-6 day')) . ' 00:00:00');
            $endtime = strtotime(date('Y-m-d') . ' 23:59:59');
            break;
        case 4:
            $starttime = strtotime($_GPC['statrtime'] . ' 00:00:00');
            $endtime = strtotime($_GPC['endtime'] . ' 23:59:59');
            break;
    }
    $condition .= ' and a.createtime>=\'' . $starttime . '\' and a.createtime<=\'' . $endtime . '\' and a.ispay=1 ';
    if ($_GPC['keyword']) {
        $keyword = $_GPC['keyword'];
        $condition .= ' and ( a.title like \'%' . $keyword . '%\' or a.goodsn like \'%' . $keyword . '%\' ) ';
    }
    $goodlist = pdo_fetchall('SELECT sum(a.total_fee) as num1,sum(a.num) as num2,b.marketprice,a.goodsn,a.title,b.pcate FROM ' . tablename('j_hmoney_ordergoods') . ' a left join ' . tablename('j_hmoney_goods') . ' b on a.goodid=b.id WHERE a.groupid =\'' . $shopid . '\' ' . $condition . ' and a.ispay=1 group by a.goodid order by num1 desc ,num2 desc');
    $condition2 = str_replace('a.', '', $condition);
    $item = pdo_fetch('SELECT sum(total_fee) as num1,sum(num) as num2 FROM ' . tablename('j_hmoney_ordergoods') . '  WHERE groupid =\'' . $shopid . '\' ' . $condition2 . ' and ispay=1 ');
    $goodary2 = array();
    $goodary3 = array();
    foreach ($goodlist as $row) {
        if (!isset($goodary2[$row['pcate']])) {
            $goodary2[$row['pcate']] = 0;
            $goodary3[$row['pcate']] = 0;
        }
        $goodary2[$row['pcate']] = $goodary2[$row['pcate']] + $row['num1'];
        $goodary3[$row['pcate']] = intval($goodary3[$row['pcate']]) + $row['num2'];
    }
} else {
    if ($operation == 'outputs') {
        $goodpcate = pdo_fetchall('SELECT id,title FROM ' . tablename('j_hmoney_goodcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by id desc');
        $goodary = array();
        foreach ($goodpcate as $row) {
            $goodary[$row['id']] = $row['title'];
        }
        $goodary[0] = '未分类';
        $datetype = $_GPC['datetype'] ? $_GPC['datetype'] : 1;
        switch ($datetype) {
            case 1:
                $starttime = strtotime(date('Y-m-d') . ' 00:00:00');
                $endtime = strtotime(date('Y-m-d') . ' 23:59:59');
                break;
            case 2:
                $starttime = strtotime(date('Y-m-d', strtotime('-1 day')) . ' 00:00:00');
                $endtime = strtotime(date('Y-m-d', strtotime('-1 day')) . ' 23:59:59');
                break;
            case 3:
                $starttime = strtotime(date('Y-m-d', strtotime('-6 day')) . ' 00:00:00');
                $endtime = strtotime(date('Y-m-d') . ' 23:59:59');
                break;
            case 4:
                $starttime = strtotime($_GPC['statrtime'] . ' 00:00:00');
                $endtime = strtotime($_GPC['endtime'] . ' 23:59:59');
                break;
        }
        $condition = ' and a.createtime>=\'' . $starttime . '\' and a.createtime<=\'' . $endtime . '\' and a.ispay=1 ';
        if ($_GPC['keyword']) {
            $keyword = $_GPC['keyword'];
            $condition .= ' and ( a.title like \'%' . $keyword . '%\' or a.goodsn like \'%' . $keyword . '%\' ) ';
        }
        $goodlist = pdo_fetchall('SELECT sum(a.total_fee) as num1,sum(a.num) as num2,b.marketprice,a.goodsn,a.title,b.pcate FROM ' . tablename('j_hmoney_ordergoods') . ' a left join ' . tablename('j_hmoney_goods') . ' b on a.goodid=b.id WHERE a.groupid =\'' . $shopid . '\' ' . $condition . ' and a.ispay=1 group by a.goodid order by num1 desc ,num2 desc');
        require_once '../framework/library/phpexcel/PHPExcel.php';
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator('智慧收银台')->setLastModifiedBy('智慧收银台')->setTitle('数据EXCEL导出')->setSubject('数据EXCEL导出')->setDescription('备份数据')->setKeywords('excel')->setCategory('result file');
        $excelAry = array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G', 7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N', 14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'X', 19 => 'T', 20 => 'U', 21 => 'V', 22 => 'W', 23 => 'X', 24 => 'Y', 25 => 'Z');
        $headerAry = array(0 => '分类', 1 => '条码', 2 => '名称', 3 => '原单价', 4 => '销售金额', 5 => '数量');
        $i = 0;
        while ($i < count($headerAry)) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($excelAry[$i] . '1', $headerAry[$i]);
            $i = $i + 1;
        }
        $num = 2;
        foreach ($goodlist as $row) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $num, $goodary[$row['pcate']])->setCellValue('B' . $num, $row['goodsn'])->setCellValue('C' . $num, $row['title'])->setCellValue('D' . $num, $row['marketprice'])->setCellValue('E' . $num, $this->_2money($row['num1']))->setCellValue('F' . $num, $row['num2']);
            $num = $num + 1;
        }
        $objPHPExcel->getActiveSheet()->setTitle('商品销售记录');
        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename="salegoods_temp.xls"');
        header('Cache-Control:max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit(0);
    }
}
include $this->template('web/' . $cfg['floder'] . '/salegoods');